import { Toast } from 'vant'
import {interfaceUtils} from './interface.js'
let crypto = require('crypto');
// https://www.npmjs.com/package/jsencrypt?activeTab=versions
import JsEncrypt from "jsencrypt";

class Login extends interfaceUtils {
    static IsRequire=2
    static Name="login"
    static TimeStamp=new Date().getTime()
    constructor(phone,loginPassward){
        super()
        this.phone=phone,
        this.loginPassward=loginPassward
        this.timeStamp=new Date().getTime()
        // this._do({account,password})
    }
    _getParams(){
        const publicKey=Login.publicKey
        let rsa = new JsEncrypt();
        rsa.setPublicKey(publicKey);
        const { phone,loginPassward } =this
        // console.log(loginPassword)
        const consumerFrom=Login.consumerFrom      
        const params=JSON.stringify( { phone,loginPassward,consumerFrom }) 
        const result= rsa.encrypt(params)  
         //    https://www.cnblogs.com/aeolian/p/12133598.html
        return encodeURIComponent(result)      
     }
     _getSign(){
        const key =Login.md5Key+""
        const {phone} =this 
        const consumerFrom=Login.consumerFrom+""
        const {timeStamp}=this
     //    https://blog.csdn.net/wail521/article/details/96430873       
        //  const sign  = this._parmaSort({ key ,phone,consumerFrom ,timeStamp  }) 
        const sign  = phone +consumerFrom+timeStamp+key
         const result = crypto.createHash('md5').update(sign).digest("hex")    
         return result 
     }
 
     _getData(){
       
         const params=this._getParams()
         const sign=this._getSign()
         const {phone} = this
         const consumerFrom=Login.consumerFrom
         const {timeStamp}=this
 
         let formData = new FormData();
         formData.append("params",params);
         formData.append("sign",sign);
         formData.append("phone",phone);
         formData.append("consumerFrom",consumerFrom);
         formData.append("timeStamp",timeStamp);
 
 
 
         return  formData
 
 
     }
 
       /**
      * 
      * @param {keyObj} 将对象排序 变成 字符串
      *  例如 a=1&b=2 
      */
     _parmaSort(keyObj){
         let keyArr = [];
       for (let key in keyObj) {
         keyArr.push(key);
       }
         let reaKryArr = keyArr.sort();
         let newArrStr = "";
         for (let i = 0; i < keyArr.length; i++) {
           newArrStr += reaKryArr[i] + "=" + keyObj[reaKryArr[i]] + "&";
         }
     
         let str = newArrStr.substr(0, newArrStr.length - 1);
         return str
     }
}

export function qualified(data){
    if(data == null || data ==""||data == undefined ){
        return false
    }else{
        return true
    }
}

export function validata(data,class_type){
    let qualify =false

    let set= Object.entries(data).slice(0,class_type.IsRequire)
    let ret= set.find((value,index)=>{
        return !qualified( value[1])
    })
    if( ret == undefined){  
        //符合要求   
        if( qualified(data.phone)  &&  qualified(data.loginPassward)  ){  
            switch (class_type.Name) {
                case 'login':
                    if((data.loginPassward+"").length<6 || (data.loginPassward+"").length>20){
                        Toast.fail({
                            message:"请输入6-20位密码"
                        })
                        qualify=false
                    }else{
                        qualify=true
                    }       
                 break;               
            }        
              return qualify
            }else{             
                return qualify
            }     
    }else{
       Toast.fail(ret[0] +" is Null");
        return false   
    }   
 


    
}


export { Login }